package com.tengju.bff.interfaces.manage.club;

import com.tengju.bff.interfaces.manage.club.facade.ZoneFacade;
import com.tengju.bff.interfaces.manage.club.vo.PageZoneInfoVO;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.KVVO;
import com.tengju.bff.interfaces.shared.SystemException;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.bff.interfaces.shared.servlet.ManageUtil;
import com.tengju.bff.interfaces.shared.servlet.NotTokenValid;
import com.tengju.user.application.archives.dto.PageQueryDTO;
import com.tengju.user.application.club.ZoneService;
import com.tengju.user.application.club.dto.AddZoneForm;
import com.tengju.user.application.club.dto.DelZoneForm;
import com.tengju.user.application.club.dto.EditZoneForm;
import com.tengju.user.application.club.dto.UserZoneRelationForm;
import com.tengju.user.application.shared.ApplicationException;
import com.tengju.user.domain.model.club.ZoneId;
import com.tengju.user.domain.model.user.Staff;
import com.tengju.user.domain.shared.DomainException;
import com.tengju.user.domain.shared.JsonUtil;
import com.tengju.user.domain.shared.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

import static com.tengju.user.domain.shared.ResultCodeEnum.SYS_EXCEPTION;

/**
 * 战区管理
 * @author chenhang
 * @Date 2021/1/12
 */
@Validated
@RestController
@Api(tags = "战区管理")
@RequestMapping(value = "manage/zone")
@Slf4j
@ManageEntrance
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ZoneController {

    @Autowired
    private ZoneService zoneService;
    @Autowired
    private ZoneFacade zoneFacade;

    @RequiresRoles("sso")
    @ApiOperation(value = "查询战区", notes = "查询战区")
    @RequestMapping(value = "queryByPage", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<PageZoneInfoVO> queryByPage(PageQueryDTO pageQueryDTO) {
        try {

            return ApiResponse.newSuccess(zoneFacade.queryByPage(pageQueryDTO));
        } catch (DomainException | ApplicationException e) {
            log.error("查询战区失败",  e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("查询战区异常", e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "添加战区", notes = "添加战区")
    @RequestMapping(value = "addZone", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> addZone(@RequestBody AddZoneForm zoneForm) {
        try {
            Staff staff = ManageUtil.getOperateUser();
            return ApiResponse.newSuccess(zoneService.addZone(zoneForm,staff));
        } catch (DomainException | ApplicationException e) {
            log.warn("添加战区失败,form:{}", JsonUtil.toJSONString(zoneForm), e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("添加战区异常,form:{}", JsonUtil.toJSONString(zoneForm), e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "编辑战区", notes = "编辑战区")
    @RequestMapping(value = "editZone", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> editZone(@RequestBody EditZoneForm editZoneForm) {
        try {
            Staff staff = ManageUtil.getOperateUser();
            return ApiResponse.newSuccess(zoneService.editZone(editZoneForm,staff));
        } catch (DomainException | ApplicationException e) {
            log.warn("编辑战区失败,form:{}", JsonUtil.toJSONString(editZoneForm), e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("编辑战区异常,form:{}", JsonUtil.toJSONString(editZoneForm), e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "删除战区", notes = "删除战区")
    @RequestMapping(value = "delZone", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> delZone(@RequestBody DelZoneForm delZoneForm) {
        try {
            return ApiResponse.newSuccess(zoneService.delZone(new ZoneId(delZoneForm.getZoneId())));
        } catch (SystemException e) {
            return ApiResponse.newFailure(e.getErrorCode(), e.getErrorMessage());
        } catch (Exception e) {
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "战区SEC查询", notes = "战区SEC查询")
    @RequestMapping(value = "/user/member/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<List<KVVO>> getUserZoneMember(@NotNull(message = "战区id不能为空") @RequestParam Long zoneId) {
        return ApiResponse.newSuccess(zoneFacade.getUserZoneMemberInfo(new ZoneId(zoneId)));
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "战区SEC保存", notes = "战区SEC保存")
    @RequestMapping(value = "/user/member/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse saveUserZoneMember(@RequestBody UserZoneRelationForm userZoneRelationForm) {
        zoneService.saveUserZoneMember(userZoneRelationForm);
        return ApiResponse.newSuccess();
    }
}
